package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.StealEachPhotosActivity;
import com.github.postsanf.yinian.activity.StealPhotoBigShowActivity;
import com.github.postsanf.yinian.activity.StealPhotosYearActivity;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedHashMap;

public class StealPhotosYearAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private BaseApplication application;
    private String year;
    private int beUserid;

    public StealPhotosYearAdapter(Context context, BaseApplication application, String year, int beUserid) {
        this.context = context;
        this.application = application;
        this.year = year;
        this.beUserid = beUserid;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return StealEachPhotosActivity.photos.size();
    }

    @Override
    public Object getItem(int position) {
        return StealEachPhotosActivity.photos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        ViewHold viewHold = new ViewHold();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.yn_item_stealphotoyear,
                    null);
            viewHold.item = (LinearLayout) convertView
                    .findViewById(R.id.stealyear_item);
            viewHold.pic = (SimpleDraweeView) convertView
                    .findViewById(R.id.stealyear_item_pic);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        viewHold.pic.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIHelper.getScreenWidth(context) / 3));
        final SimpleDraweeView imageView = viewHold.pic;
        final String photourl = StealEachPhotosActivity.photos.get(position).getUrl();
        Log.i("xjs","photourl==>"+photourl);
        if (!StringUtils.isEmpty(photourl)) {
            ImageOptHelper.showFrescoStatus(Uri.parse(photourl), viewHold.pic, context);
            ScaleAnimation animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(2000);
            viewHold.pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StealPhotoBigShowActivity.class);
                    intent.putExtra("url", photourl);
                    context.startActivity(intent);
                }
            });
        } else {
            ImageOptHelper.showFrescoStatus(Uri.parse("res://" + context.getPackageName() + "/" + R.drawable.stealphotoyear_item), viewHold.pic, context);
            viewHold.pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (StealPhotosYearActivity.total == 0) {
                        Toast.makeText(context, "每天最多只能看三张", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    PeepUserPhoto(imageView, position, photourl);
                    StealPhotosYearActivity.total--;
                }
            });
        }
        return convertView;
    }

    class ViewHold {
        public LinearLayout item;
        public SimpleDraweeView pic;
    }

    private void PeepUserPhoto(final SimpleDraweeView imageView, final int position, final String photourl) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        mParam.put("beUserid", String.valueOf(beUserid));
        mParam.put("groupid", String.valueOf(application.getCurGid()));
        mParam.put("position", String.valueOf(position));
        mParam.put("year", year);
        String urlsto = context.getString(R.string.SLIVER_URL) + "PeepUserPhoto";
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "PeepUserPhoto===>" + s.toString());
                        try {
                            JSONObject result = new JSONObject(s);
                            if (result.optInt("code") == 0) {
                                String url = result.optJSONArray("data").optJSONObject(0).optString("photo");
                                StealEachPhotosActivity.photos.get(position).setUrl(url);
                                ImageOptHelper.showFrescoStatus(Uri.parse(url), imageView, context);
                                Intent intent = new Intent(context, StealPhotoBigShowActivity.class);
                                intent.putExtra("url", url);
                                context.startActivity(intent);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        LiteHttp.newApacheHttpClient(new HttpConfig(context)).executeAsync(req);
    }

}
